<template>
    <div class="reportFormPage" v-if="show">
        <div id="domid" class="report-dom">
            <!-- 头部标题区 -->
            <el-row>
                <el-col :span="5" :offset="19">
                    <div class="pageNumBox">
                        <span>第</span>
                        <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
                        <span>页，共</span>
                        <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
                        <span>页</span>
                    </div>
                </el-col>
            </el-row>
            <div class="topTwoBox">
                <div class="centerName">{{ form.baobmc }}</div>
                <div class="codeText">{{ form.baobbh }}</div>
            </div>
            <div class="threeTopBox">
                <div class="left-border">
                    <div class="box-title">检测单位名称：</div>
                    <div class>
                        {{
                        form.jiancdwmc
                        }}
                    </div>
                </div>
                <div class="right-border">
                    <span>记录编号：</span>
                    <span class="placeholder">{{ form.jilbh }}</span>
                </div>
            </div>
            <!-- 表格主要内容区 -->
            <div class="table-border">
                <!-- 主要内容表格 -->
                <table height="220px" class="top-table">
                    <tbody>
                    <tr>
                        <td height="30" align="center" width="15%">
                            <span style="width: 90px; display: inline-block">工程名称</span>
                        </td>
                        <td width="35%" class="text-center">
                            <span>{{ form.gongcmc }}</span>
                        </td>
                        <td align="center" width="15%">
                            <span style="width: 90px; display: inline-block">工程部位/用途</span>
                        </td>
                        <td width="35%" class="text-center">
                            <span>{{ form.gongcbwyt }}</span>
                        </td>
                    </tr>
                    <tr>
                        <td height="50" align="center">样品信息</td>
                        <td class="specimen-border" colspan="3">
                            <span>样品名称：</span>
                            <span :class="{ placeholder: !form.yangpmc }">{{ form.yangpmc }}</span>
                            <span>；样品编号：</span>
                            <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>
                            <span>；样品数量：</span>
                            <span :class="{ placeholder: !form.yangpsl }">{{ form.yangpsl }}</span>
                            <span>；样品状态：</span>
                            <span :class="{ placeholder: !form.yangpzt }">{{ form.yangpzt }}</span>
                            <span>；来样时间：</span>
                            <span :class="{ placeholder: !form.yangpsj }">{{ form.yangpsj }}</span>
                        </td>
                    </tr>

                    <tr>
                        <td height="25" align="center" width="15%">
                            <span style="width: 90px; display: inline-block">试验检测日期</span>
                        </td>
                         <!-- <td width="35%" class="text-center">{{ form.shiyjcrq }}</td> -->                                            
 <td width="35%" class="text-center">
      <el-input v-model="form.shiyjcrq" />
</td>
                        <td align="center" width="15%">
                            <span style="width: 90px; display: inline-block">试验条件</span>
                        </td>
                        <td v-if="isPdf" width="35%">
                            温度：
                            <span style="display: inline-block; width: 10%">{{ form.shiytjwd }}</span>
                            ℃；湿度：
                            <span style="display: inline-block; width: 10%">{{ form.shiytjsd }}</span
                            >%RH
                        </td>
                        <td v-else width="35%">
                            温度：
                            <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjwd" @change="backBgzFun" style="width: 15%"/>
                            ℃；湿度：
                            <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjsd" @change="backBgzFun" style="width: 15%"/>
                            %RH
                        </td>
                    </tr>

                    <tr>
                        <td height="50" align="center" width="15%">
                            <span style="width: 90px; display: inline-block">检测依据</span>
                        </td>
                        <td width="35%">
                            <span v-if="isPdf">{{ form.jiancyj }}</span>
                            <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark"
                                      @dblclick.native="showBsJiancyjView" readonly/>
                        </td>
                        <td align="center" width="15%">
                            <span style="width: 90px; display: inline-block">判定依据</span>
                        </td>
                        <td width="35%">
                            <span v-if="isPdf">{{ form.pandyj }}</span>
                            <el-input v-else type="textarea" v-model="form.pandyj" :rows="2"
                                      @dblclick.native="showBsJudgeBaseView" @focus="handleFocus('pandyj')"
                                      class="mark"/>
                        </td>
                    </tr>
                    <tr class="bottom-border">
                        <td height="50" align="center">
                            <div>主要仪器设备</div>
                            <div>名称及编号</div>
                        </td>
                        <td colspan="3">
                            <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                            <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh"
                                      class="height100 width100 mark" @dblclick.native="showBsInstrumentView"
                                      readonly></el-input>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <!-- 单轴抗压强度试验-->
                <table class="middle-table" style="height: 60px">
                    <tbody>
                    <tr>
                        <td class="text-center" style="width: 100%;height: 30px;" colspan="13">
                            <span style="width:100px;display:inline-block">单轴抗压强度试验</span>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center" style="width: 15%;" height="30" colspan="2">
                            <span style="width:90px;display:inline-block">试样用途</span>
                        </td>
                        <td class="text-center" style="width: 20%;" colspan="3">
                            <el-input type="textarea"
                                      v-model="form.shiyyt"
                                      @change="backBgzFun"
                                      @keydown.native="handleKeyDown($event, 0, 0)"
                                      :ref="'0_0'"
                                      :readonly="isPdf"
                            />
                        </td>
                        <td class="text-center" style="width: 17%;" colspan="3">
                            <span style="width:90px;display:inline-block">岩石层理描述</span>
                        </td>
                        <td class="text-center" style="width: 15%;" colspan="2">
                            <el-input type="textarea"
                                      v-model="form.yansclms"
                                      @change="backBgzFun"
                                      @keydown.native="handleKeyDown($event, 0, 1)"
                                      :ref="'0_1'"
                                      :readonly="isPdf"
                            />
                        </td>
                        <td class="text-center" style="width: 13%;">
                            <span style="width:90px;display:inline-block">试件形状</span>
                        </td>
                        <td class="text-center" style="width: 20%;" colspan="2">
                            <el-input type="textarea"
                                      v-model="form.shijxz"
                                      @change="backBgzFun"
                                      @keydown.native="handleKeyDown($event, 0, 2)"
                                      :ref="'0_2'"
                                      :readonly="isPdf"
                            />
                        </td>
                    </tr>
                    </tbody>
                </table>
                <table class="middle-table" style="height: 420px;">
                    <tbody>
                    <tr>
                        <td class="text-center" style="width: 6%;" height="60" rowspan="2">
                            <span style="width:30px;display:inline-block">试件编号</span>
                        </td>
                        <td class="text-center" style="width: 26%;" colspan="4">
                            <span style="width:90px;display:inline-block">顶面边长(mm)</span>
                        </td>
                        <td class="text-center" style="width: 26%;" colspan="4">
                            <span style="width:90px;display:inline-block">底面边长(mm)</span>
                        </td>
                        <td class="text-center" style="width: 10%;" rowspan="2">
                            <span style="width:40px;display:inline-block">截面积(mm³)</span>
                        </td>
                        <td class="text-center" style="width: 10%;" rowspan="2">
                            <span style="width:40px;display:inline-block">试件破坏荷载(kN)</span>
                        </td>
                        <td class="text-center" style="width: 10%;" rowspan="2">
                            <span style="display:inline-block">抗压强度(MPa)</span>
                        </td>
                        <td class="text-center" style="width: 12%;" rowspan="2">
                            <span style="display:inline-block">平均抗压强度(MPa)</span>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center" height="30">1</td>
                        <td class="text-center">2</td>
                        <td class="text-center">3</td>
                        <td class="text-center">4</td>
                        <td class="text-center">1</td>
                        <td class="text-center">2</td>
                        <td class="text-center">3</td>
                        <td class="text-center">4</td>
                    </tr>
                    <template v-for="(item, index) in form.baDetailVos">
                        <tr>
                            <td style="width: 3%;" height="30">{{ item.shijbh }}</td>
                            <td>
                                <el-input type="number"
                                          v-model="item.dingmbc1"
                                          @keydown.native="handleKeyDown($event, index+1, 0)"
                                          @paste.native="handlePasteList($event,  index, 0)"
                                          :ref="`${(index+1)}_0`"
                                          :readonly="isPdf"
                                ></el-input>
                            </td>
                            <td>
                                <el-input type="number"
                                          v-model="item.dingmbc2"
                                          @keydown.native="handleKeyDown($event, index+1, 1)"
                                          @paste.native="handlePasteList($event,  index, 1)"
                                          :ref="`${(index+1)}_1`"
                                          :readonly="isPdf"
                                ></el-input>
                            </td>
                            <td>
                                <el-input type="number"
                                          v-model="item.dingmbc3"
                                          @keydown.native="handleKeyDown($event, index+1, 2)"
                                          @paste.native="handlePasteList($event,  index, 2)"
                                          :ref="`${(index+1)}_2`"
                                          :readonly="isPdf"
                                ></el-input>
                            </td>
                            <td>
                                <el-input type="number"
                                          v-model="item.dingmbc4"
                                          @keydown.native="handleKeyDown($event, index+1, 3)"
                                          @paste.native="handlePasteList($event,  index, 3)"
                                          :ref="`${(index+1)}_3`"
                                          :readonly="isPdf"></el-input>
                            </td>
                            <td>
                                <el-input type="number"
                                          v-model="item.dimbc1"
                                          @keydown.native="handleKeyDown($event, index+1, 4)"
                                          @paste.native="handlePasteList($event,  index, 4)"
                                          :ref="`${(index+1)}_4`"
                                          :readonly="isPdf"></el-input>
                            </td>
                            <td>
                                <el-input type="number"
                                          v-model="item.dimbc2"
                                          @keydown.native="handleKeyDown($event, index+1, 5)"
                                          @paste.native="handlePasteList($event,  index, 5)"
                                          :ref="`${(index+1)}_5`"
                                          :readonly="isPdf"></el-input>
                            </td>
                            <td>
                                <el-input type="number"
                                          v-model="item.dimbc3"
                                          @keydown.native="handleKeyDown($event, index+1, 6)"
                                          @paste.native="handlePasteList($event,  index, 6)"
                                          :ref="`${(index+1)}_6`"
                                          :readonly="isPdf"></el-input>
                            </td>
                            <td>
                                <el-input type="number"
                                          v-model="item.dimbc4"
                                          @keydown.native="handleKeyDown($event, index+1, 7)"
                                          @paste.native="handlePasteList($event,  index, 7)"
                                          :ref="`${(index+1)}_7`"
                                          :readonly="isPdf"></el-input>
                            </td>
                            <td>
                                <el-input type="number"
                                          v-model="item.jiemj"
                                          @change="computeKyqd(item)"
                                          @keydown.native="handleKeyDown($event, index+1, 8)"
                                          @paste.native="handlePasteList($event,  index, 8)"
                                          :ref="`${(index+1)}_8`"
                                          :readonly="isPdf"
                                ></el-input>
                            </td>
                            <td>
                                <el-input type="number"
                                          v-model="item.shijphzh"
                                          @change="computeKyqd(item)"
                                          @keydown.native="handleKeyDown($event, index+1, 9)"
                                          @paste.native="handlePasteList($event,  index, 9)"
                                          :ref="`${(index+1)}_9`"
                                          :readonly="isPdf"
                                ></el-input>
                            </td>
                            <td>
                                <span>{{ item.kangyqd }}</span>
                            </td>
                            <td style="width: 12%;" v-if="index === 0" rowspan="6">{{ form.pingjkyqd1 }}</td>
                            <td style="width: 12%;" v-if="index === 6" rowspan="6">{{ form.pingjkyqd2 }}</td>
                        </tr>
                    </template>
                    </tbody>
                </table>
                <!-- 附加声明 -->
                <table height="120px" class="bottom-table">
                    <tr>
                        <td>
                            <el-input type="textarea" :rows="2" v-model="form.fujsm" :readonly="isPdf"/>
                        </td>
                    </tr>
                </table>
            </div>
            <!-- 表格底部签名区域 -->
            <table height="22px" class="transparent">
                <tbody>
                <tr>
                    <td width="10%" align="right">检测：</td>
                    <td width="15%" align="left"></td>
                    <td width="10%" align="right">记录：</td>
                    <td width="15%" align="left"></td>
                    <td width="10%" align="right">复核：</td>
                    <td align="left" width="15%"></td>
                    <td width="10%">日期：</td>
                    <td align="left" width="15%">
                        <div style="display: inline-block; margin-left: 20px">年</div>
                        <div style="display: inline-block; margin-left: 20px">月</div>
                        <div style="display: inline-block ;margin-left: 20px">日</div>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
        <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
        <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
    </div>
</template>

<script>
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";

import Bus from "@/utils/bus";
import {getCjlkzhhljlbBsInfo} from "@/api/report_jl/cjl";
import {handleFocus, handleKeyDown, handlePaste} from "@/views/reportForms/utils";
import decimal from "@/utils/big-decimal";
import {addYsDzkyqdsyBsInfo, getYsDzkyqdsyBsInfo, updateYsDzkyqdsyBsInfo} from "@/api/reportys/ysYsdzkyqdsyjcjlbBsInfo";

export default {
    name: "dzkyqdsy",
    components: {BsJudgeBaseView, BsInstrumentView, BsJiancyjView},
    props: {
        excelId: {
            type: [String, Number],
            default: () => ""
        },
        syType: {
            type: [String, Number],
            default: () => ""
        },
        page: {
            type: Number,
            default: () => 0
        },
        isPdf: {
            type: Boolean,
            default: () => false
        },
        printEmpty: {
            type: Boolean,
            default: () => false
        }
    },
    data() {
        return {
            show: false,
            form: {
                excelId: undefined,
                totalPage: undefined,
                page: undefined,
                jilbh: undefined,
                jiancdwmc: undefined,
                gongcmc: undefined,
                gongcbwyt: undefined,
                yangpmc: undefined,
                yangpbh: undefined,
                yangpsl: undefined,
                yangpzt: undefined,
                yangpsj: undefined,
                shiyjcrq: undefined,
                shiytjwd: undefined,
                shiytjsd: undefined,
                jiancyj: undefined,
                pandyj: undefined,
                jiancyjIds: [],
                pandyjIds: [],
                zhuyyqsbmcjbh: undefined,
                shebIds: [],
                fujsm: undefined,
                shiyyt: undefined,
                yansclms: undefined,
                shijxz: undefined,
                pingjkyqd1: undefined,
                pingjkyqd2: undefined,
                baDetailVos: []
            }
        };
    },
    created() {
        this.init();
    },
    methods: {
        init() {
            this.show = false;
            getYsDzkyqdsyBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
                this.form = response.data;
                if (!this.form.fujsm) {
                    this.form.fujsm = "附加声明：";
                }
                this.setInitialValue();
                this.show = true;
            });
        },
        // 设置初始值
        setInitialValue() {
            if (this.form) {
                this.form.baobmc = this.form.baobmc || "岩石单轴抗压强度试验检测记录表(立方体)";
                this.form.baobbh = this.form.baobbh || "JGL0030011";

                if (!this.form.baDetailVos || this.form.baDetailVos.length <= 0) {
                    this.setDetailList();
                } else {
                    this.form.baDetailVos = this.form.baDetailVos.map((item, index) => {
                        return {
                            ...item,
                            shijbh: index + 1
                        }
                    })
                }
            }

        },
        // 设置内容列表初始值
        setDetailList() {
            this.$set(this.form, "baDetailVos", []);
            for (let i = 0; i < 12; i++) {
                const detail = {
                    id: undefined,
                    excelId: undefined,
                    shijbh: i + 1,
                    dingmbc1: undefined,
                    dingmbc2: undefined,
                    dingmbc3: undefined,
                    dingmbc4: undefined,
                    dimbc1: undefined,
                    dimbc2: undefined,
                    dimbc3: undefined,
                    dimbc4: undefined,
                    jiemj: undefined,
                    shijphzh: undefined,
                    kangyqd: undefined,
                };
                this.form.baDetailVos.push(detail);
            }
        },
        // 显示仪器列表
        showBsInstrumentView() {
            this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
        },
        // 仪器选择
        selectInstrument(info) {
            this.form.zhuyyqsbmcjbh = info.label;
            this.form.shebIds = info.ids;
            Bus.$emit("setYsJcBgZhuyyqsbmcjbh", {zhuyyqsbmcjbh: this.form.zhuyyqsbmcjbh, shebIds: this.form.shebIds});
        },
        // 显示检测依据列表
        showBsJiancyjView() {
            this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
        },
        // 检测依据选择
        selectJiancyj(info) {
            this.form.jiancyj = info.label;
            this.form.jiancyjIds = info.ids;
            Bus.$emit("setYsJcBgJiancyj", {jiancyj: this.form.jiancyj, jiancyjIds: this.form.jiancyjIds});
        },
        // 显示判定依据列表
        showBsJudgeBaseView() {
            this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
        },
        // 判断依据选择
        selectJudge(info) {
            if (!this.form.pandyj) {
                this.form.pandyj = info.label;
                return;
            }
            if (info && info.label) {
                this.form.pandyj += "、" + info.label;
            }
        },
        handleFocus(event) {
            handleFocus(event, this.page, this.form[event]);
        },

        // 计算抗压强度
        computeKyqd(item) {
            if (!item.jiemj || !item.shijphzh) {
                item.kangyqd = null;
                this.computePjkyqd();
            } else {
                item.kangyqd = decimal.divide(decimal.multiply(item.shijphzh, 1000), item.jiemj)
                this.computePjkyqd();
            }
        },
        // 计算平均抗压强度
        computePjkyqd() {
            let count1 = 0
            let sum1 = 0

            let count2 = 0
            let sum2 = 0
            if (this.form.baDetailVos && this.form.baDetailVos.length > 0) {
                this.form.baDetailVos.forEach((item, index) => {
                    if (index <= 5) {
                        if (item.kangyqd) {
                            count1++;
                            sum1 = decimal.add(sum1, item.kangyqd);
                        }
                    } else {
                        if (item.kangyqd) {
                            count2++;
                            sum2 = decimal.add(sum2, item.kangyqd);
                        }
                    }
                })
                if (count1) {
                    this.form.pingjkyqd1 = decimal.divide(sum1, count1)
                    this.setTgBgPingjkyqd()
                }
                if (count2) {
                    this.form.pingjkyqd2 = decimal.divide(sum2, count2)
                    this.setTgBgPingjkyqd()
                }
            }
        },
        // 检测报告表传值平均抗压强度
        setTgBgPingjkyqd() {
            Bus.$emit("setYsTgBgPingjkyqd", [
                {key: 'pingjkyqd', value: this.form.pingjkyqd1},
                {key: 'pingjkyqd', value: this.form.pingjkyqd2}]
            );
        },
        // 返回报告引入值
        backBgzFun() {
            Bus.$emit("setYsBgBase", {
                shiytjwd:this.form.shiytjwd,
                shiytjsd:this.form.shiytjsd,

                shiyyt:this.form.shiyyt,
                yansclms:this.form.yansclms,
                shijxz:this.form.shijxz,
            });
        },

        // 提交表单
        submitForm() {
            return this.form;
        },

        // 鼠标移动操作
        handleKeyDown(event, rowIndex, colIndex) {
            const maxArr = [2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]; // 每一行最多有多少可聚焦输入框（从0开始）
            const ref = handleKeyDown(event, rowIndex, colIndex, maxArr);
            if (this.$refs[ref]?.length) {
                this.$refs[ref][0].focus();
            } else if (this.$refs[ref]) {
                this.$refs[ref].focus();
            }
        },
        // List粘贴赋值方法
        handlePasteList(event, rowIndex, colIndex) {
            const nameAllArr = this.form.baDetailVos.map((item, index) => {
                return [
                    "dingmbc1", "dingmbc2",
                    "dingmbc3", "dingmbc4",
                    "dimbc1", "dimbc2",
                    "dimbc3", "dimbc4",
                    "jiemj", "shijphzh",
                ]
            });
            const subscriptArr = this.form.baDetailVos.map((item, index) => {
                return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
            });
            const methodsAllArr = this.form.baDetailVos.map((item, index) => {
                return [
                    null, null,
                    null, null,
                    null, null,
                    null, null,
                    "computeKyqd", "computeKyqd",
                ]
            });
            const parameterArr = this.form.baDetailVos.map((item, index) => {
                return [
                    null, null,
                    null, null,
                    null, null,
                    null, null,
                    item, item,
                ]
            });

            const data = handlePaste(event, rowIndex, colIndex, nameAllArr, methodsAllArr, parameterArr, subscriptArr);
            const {cols, nameSetArr, methodsSetArr, parameterSetArr, subscriptSetArr} = data;
            for (let a = 0; a < nameSetArr.length; a++) {
                for (let b = 0; b < nameSetArr[a].length; b++) {
                    this.form.baDetailVos[rowIndex + a][nameSetArr[a][b]] = cols[a][b];
                }
            }
            for (let a = 0; a < methodsSetArr.length; a++) {
                for (let b = 0; b < methodsSetArr[a].length; b++) {
                    if (methodsSetArr[a][b]) {
                        this.setCalculate(methodsSetArr[a][b], parameterSetArr[a][b]);
                    }

                }
            }
        },

        // 计算方法调用
        setCalculate(name, a, b, c, d, e) {
            this[name](a, b, c, d, e);
        }
    }
}
</script>

<style scoped lang="scss">
.td-input {
  width: 100% !important;
  height: 30px !important;
}

</style>
